package nl.depository.pim.services;

import nl.depository.pim.entities.User;
import nl.depository.pim.security.AuthenticationException;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Session;

public class BasicAuthenticator implements Authenticator
{
	public static final String AUTH_TOKEN = "authToken";
	
	@Inject
	private Request request;
	
	public void login(String username, String password) throws AuthenticationException
	{
		User user = null;
		
		if (username.equals("admin") && password.equals("admin123"))
		{
			user = new User();
			user.setFullName("Matthijs Steen");
			user.setUsername("admin");
			user.setPassword("admin123");
		}
		
		if (user == null)
		{
			throw new AuthenticationException("The user doesn't exist");
		}
		
		request.getSession(true).setAttribute(AUTH_TOKEN, user);
	}
	
	public boolean isLoggedIn()
	{
		Session session = request.getSession(false);
		if (session != null)
		{
			return session.getAttribute(AUTH_TOKEN) != null;
		}
		return false;
	}
	
	public void logout()
	{
		Session session = request.getSession(false);
		if (session != null)
		{
			session.setAttribute(AUTH_TOKEN, null);
			session.invalidate();
		}
	}
	
	public User getLoggedUser()
	{
		User user = null;
		
		if (isLoggedIn())
		{
			user = (User) request.getSession(true).getAttribute(AUTH_TOKEN);
		}
		else
		{
			throw new IllegalStateException("The user is not logged ! ");
		}
		
		return user;
	}
}
